package cn.flying.cloud.task.core.handler;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.task.core.model.JobContext;
import cn.flying.cloud.task.core.scheduler.SchedBatchJob;
import cn.flying.cloud.task.core.scheduler.SchedQuartzJob;

/**
 * @author: admin
 * @date: 2025年03月12日 20:50
 * @version: 1.0
 */
@RestController
@RequestMapping("/job")
public class JobHandlerController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SchedQuartzJob schedQuartzJob;
    @Resource
    private SchedBatchJob schedBatchJob;

    /**
     * 执行Job
     *
     * @param data
     */
    @RequestMapping("/run")
    public Rt<?> run(@RequestBody String data) {
        JobContext context = Ft.JSON.deserialize(data, JobContext.class);
        if ("DEFAULT".equals(context.getJobType())) {
            logger.info("执行定时任务：【{}】", context.getJobName());
            schedQuartzJob.run(context);
        } else {
            logger.info("执行批量任务：【{}】", context.getJobName());
            schedBatchJob.run(context);
        }
        return Rt.success("调度成功！");
    }

//    /**
//     * 补偿Job
//     * @param data
//     */
//    @RequestMapping("/resolve")
//    public Rt<?> resolve(@RequestBody String data) {
//        JobContext context = Ft.JSON.deserialize(data, JobContext.class);
//        if ("DEFAULT".equals(context.getJobType())) {
//            logger.info("执行定时任务：【{}】", context.getJobName());
//            schedQuartzJob.run(context);
//        } else {
//            logger.info("执行批量任务：【{}】", context.getJobName());
//            schedBatchJob.run(context);
//        }
//        return Rt.success("调度成功！");
//    }

    @RequestMapping("/stop")
    public Rt<?> stop(@RequestBody String data) {
        JobContext context = Ft.JSON.deserialize(data, JobContext.class);
        logger.info("停止批量任务：【{}】", context.getJobName());
        schedBatchJob.stop(context);
        return Rt.success("停止成功！");
    }
}
